package cn.edu.scau.cmi.hombio.web.rest;

import cn.edu.scau.cmi.hombio.dao.MedicinePriceDAO;
import cn.edu.scau.cmi.hombio.dao.MedicineStorageDAO;
import cn.edu.scau.cmi.hombio.dao.MedicineSupplierDAO;

import cn.edu.scau.cmi.hombio.domain.MedicinePrice;
import cn.edu.scau.cmi.hombio.domain.MedicineStorage;
import cn.edu.scau.cmi.hombio.domain.MedicineSupplier;

import cn.edu.scau.cmi.hombio.service.MedicinePriceService;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.WebDataBinder;

import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Spring Rest controller that handles CRUD requests for MedicinePrice entities
 * 
 */

@Controller("MedicinePriceRestController")

public class MedicinePriceRestController {

	/**
	 * DAO injected by Spring that manages MedicinePrice entities
	 * 
	 */
	@Autowired
	private MedicinePriceDAO medicinePriceDAO;

	/**
	 * DAO injected by Spring that manages MedicineStorage entities
	 * 
	 */
	@Autowired
	private MedicineStorageDAO medicineStorageDAO;

	/**
	 * DAO injected by Spring that manages MedicineSupplier entities
	 * 
	 */
	@Autowired
	private MedicineSupplierDAO medicineSupplierDAO;

	/**
	 * Service injected by Spring that provides CRUD operations for MedicinePrice entities
	 * 
	 */
	@Autowired
	private MedicinePriceService medicinePriceService;

	/**
	 * Delete an existing MedicineStorage entity
	 * 
	 */
	@RequestMapping(value = "/MedicinePrice/{medicineprice_id}/medicineStorage/{medicinestorage_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteMedicinePriceMedicineStorage(@PathVariable Integer medicineprice_id, @PathVariable Integer related_medicinestorage_id) {
		medicinePriceService.deleteMedicinePriceMedicineStorage(medicineprice_id, related_medicinestorage_id);
	}

	/**
	* Get MedicineSupplier entity by MedicinePrice
	* 
	*/
	@RequestMapping(value = "/MedicinePrice/{medicineprice_id}/medicineSupplier", method = RequestMethod.GET)
	@ResponseBody
	public MedicineSupplier getMedicinePriceMedicineSupplier(@PathVariable Integer medicineprice_id) {
		return medicinePriceDAO.findMedicinePriceByPrimaryKey(medicineprice_id).getMedicineSupplier();
	}

	/**
	* Delete an existing MedicinePrice entity
	* 
	*/
	@RequestMapping(value = "/MedicinePrice/{medicineprice_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteMedicinePrice(@PathVariable Integer medicineprice_id) {
		MedicinePrice medicineprice = medicinePriceDAO.findMedicinePriceByPrimaryKey(medicineprice_id);
		medicinePriceService.deleteMedicinePrice(medicineprice);
	}

	/**
	* Create a new MedicineStorage entity
	* 
	*/
	@RequestMapping(value = "/MedicinePrice/{medicineprice_id}/medicineStorage", method = RequestMethod.POST)
	@ResponseBody
	public MedicineStorage newMedicinePriceMedicineStorage(@PathVariable Integer medicineprice_id, @RequestBody MedicineStorage medicinestorage) {
		medicinePriceService.saveMedicinePriceMedicineStorage(medicineprice_id, medicinestorage);
		return medicineStorageDAO.findMedicineStorageByPrimaryKey(medicinestorage.getId());
	}

	/**
	* View an existing MedicineSupplier entity
	* 
	*/
	@RequestMapping(value = "/MedicinePrice/{medicineprice_id}/medicineSupplier/{medicinesupplier_id}", method = RequestMethod.GET)
	@ResponseBody
	public MedicineSupplier loadMedicinePriceMedicineSupplier(@PathVariable Integer medicineprice_id, @PathVariable Integer related_medicinesupplier_id) {
		MedicineSupplier medicinesupplier = medicineSupplierDAO.findMedicineSupplierByPrimaryKey(related_medicinesupplier_id, -1, -1);

		return medicinesupplier;
	}

	/**
	* Save an existing MedicinePrice entity
	* 
	*/
	@RequestMapping(value = "/MedicinePrice", method = RequestMethod.PUT)
	@ResponseBody
	public MedicinePrice saveMedicinePrice(@RequestBody MedicinePrice medicineprice) {
		medicinePriceService.saveMedicinePrice(medicineprice);
		return medicinePriceDAO.findMedicinePriceByPrimaryKey(medicineprice.getId());
	}

	/**
	* Save an existing MedicineSupplier entity
	* 
	*/
	@RequestMapping(value = "/MedicinePrice/{medicineprice_id}/medicineSupplier", method = RequestMethod.PUT)
	@ResponseBody
	public MedicineSupplier saveMedicinePriceMedicineSupplier(@PathVariable Integer medicineprice_id, @RequestBody MedicineSupplier medicinesupplier) {
		medicinePriceService.saveMedicinePriceMedicineSupplier(medicineprice_id, medicinesupplier);
		return medicineSupplierDAO.findMedicineSupplierByPrimaryKey(medicinesupplier.getId());
	}

	/**
	* Create a new MedicinePrice entity
	* 
	*/
	@RequestMapping(value = "/MedicinePrice", method = RequestMethod.POST)
	@ResponseBody
	public MedicinePrice newMedicinePrice(@RequestBody MedicinePrice medicineprice) {
		medicinePriceService.saveMedicinePrice(medicineprice);
		return medicinePriceDAO.findMedicinePriceByPrimaryKey(medicineprice.getId());
	}

	/**
	* View an existing MedicineStorage entity
	* 
	*/
	@RequestMapping(value = "/MedicinePrice/{medicineprice_id}/medicineStorage/{medicinestorage_id}", method = RequestMethod.GET)
	@ResponseBody
	public MedicineStorage loadMedicinePriceMedicineStorage(@PathVariable Integer medicineprice_id, @PathVariable Integer related_medicinestorage_id) {
		MedicineStorage medicinestorage = medicineStorageDAO.findMedicineStorageByPrimaryKey(related_medicinestorage_id, -1, -1);

		return medicinestorage;
	}

	/**
	* Show all MedicinePrice entities
	* 
	*/
	@RequestMapping(value = "/MedicinePrice", method = RequestMethod.GET)
	@ResponseBody
	public List<MedicinePrice> listMedicinePrices() {
		return new java.util.ArrayList<MedicinePrice>(medicinePriceService.loadMedicinePrices());
	}

	/**
	* Select an existing MedicinePrice entity
	* 
	*/
	@RequestMapping(value = "/MedicinePrice/{medicineprice_id}", method = RequestMethod.GET)
	@ResponseBody
	public MedicinePrice loadMedicinePrice(@PathVariable Integer medicineprice_id) {
		return medicinePriceDAO.findMedicinePriceByPrimaryKey(medicineprice_id);
	}

	/**
	* Save an existing MedicineStorage entity
	* 
	*/
	@RequestMapping(value = "/MedicinePrice/{medicineprice_id}/medicineStorage", method = RequestMethod.PUT)
	@ResponseBody
	public MedicineStorage saveMedicinePriceMedicineStorage(@PathVariable Integer medicineprice_id, @RequestBody MedicineStorage medicinestorage) {
		medicinePriceService.saveMedicinePriceMedicineStorage(medicineprice_id, medicinestorage);
		return medicineStorageDAO.findMedicineStorageByPrimaryKey(medicinestorage.getId());
	}

	/**
	* Register custom, context-specific property editors
	* 
	*/
	@InitBinder
	public void initBinder(WebDataBinder binder, HttpServletRequest request) { // Register static property editors.
		binder.registerCustomEditor(java.util.Calendar.class, new org.skyway.spring.util.databinding.CustomCalendarEditor());
		binder.registerCustomEditor(byte[].class, new org.springframework.web.multipart.support.ByteArrayMultipartFileEditor());
		binder.registerCustomEditor(boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(false));
		binder.registerCustomEditor(Boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(true));
		binder.registerCustomEditor(java.math.BigDecimal.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(java.math.BigDecimal.class, true));
		binder.registerCustomEditor(Integer.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Integer.class, true));
		binder.registerCustomEditor(java.util.Date.class, new org.skyway.spring.util.databinding.CustomDateEditor());
		binder.registerCustomEditor(String.class, new org.skyway.spring.util.databinding.StringEditor());
		binder.registerCustomEditor(Long.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Long.class, true));
		binder.registerCustomEditor(Double.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Double.class, true));
	}

	/**
	* Create a new MedicineSupplier entity
	* 
	*/
	@RequestMapping(value = "/MedicinePrice/{medicineprice_id}/medicineSupplier", method = RequestMethod.POST)
	@ResponseBody
	public MedicineSupplier newMedicinePriceMedicineSupplier(@PathVariable Integer medicineprice_id, @RequestBody MedicineSupplier medicinesupplier) {
		medicinePriceService.saveMedicinePriceMedicineSupplier(medicineprice_id, medicinesupplier);
		return medicineSupplierDAO.findMedicineSupplierByPrimaryKey(medicinesupplier.getId());
	}

	/**
	* Delete an existing MedicineSupplier entity
	* 
	*/
	@RequestMapping(value = "/MedicinePrice/{medicineprice_id}/medicineSupplier/{medicinesupplier_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteMedicinePriceMedicineSupplier(@PathVariable Integer medicineprice_id, @PathVariable Integer related_medicinesupplier_id) {
		medicinePriceService.deleteMedicinePriceMedicineSupplier(medicineprice_id, related_medicinesupplier_id);
	}

	/**
	* Get MedicineStorage entity by MedicinePrice
	* 
	*/
	@RequestMapping(value = "/MedicinePrice/{medicineprice_id}/medicineStorage", method = RequestMethod.GET)
	@ResponseBody
	public MedicineStorage getMedicinePriceMedicineStorage(@PathVariable Integer medicineprice_id) {
		return medicinePriceDAO.findMedicinePriceByPrimaryKey(medicineprice_id).getMedicineStorage();
	}
}